const axios = require('axios')
const qs = require('querystring')
const result = require('./handle')

let params = qs.stringify({
  grant_type: 'client_credential',
  appid: 'wxa97823b942058b03',
  secret: 'ba532f8d0567a216213469eaf1d7493b',
})
let url = 'https://api.weixin.qq.com/cgi-bin/token?' + params
let env = 'cloud1-7gqr8yw7aaafbdec'
let Addurl = 'https://api.weixin.qq.com/tcb/databaseadd?access_token='
let TripUrl="https://api.weixin.qq.com/tcb/databasequery?access_token="
class getToken {
  //获取token
  async getToken() {
    try {
      let token = await axios.get(url)
      // console.log(token)
      if (token.status == 200) {
        return token.data.access_token
      } else {
        throw '获取token错误'
      }
    } catch (error) {
      throw new result(error, 500)
    }
  }

// 调用云开发的http api接口
async posteve(dataurl, query) {
  try {
    let token = await this.getToken()
    let data = await axios.post(dataurl + token, { env, query })
     if(data.data.errcode==0){
        return data.data
     }else{
        throw "请求出错"
     }
  } catch (error) {
    throw new result(error,500)
  }
}
}
module.exports = { getToken,Addurl ,TripUrl}
